package com.example.mdoncall;

import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author Anthony Maniquiz & Laine Nicolas (Smart Telecom Inc. OJT) 
 * @version 1
 * 
 */

public class EditPatientActivity extends Activity{
	
	int search;
	String condition;
	String allergy;
	String medication;
	int idConfirm;
	
	
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		setContentView(R.layout.activity_editpatient);
		setOrientation();
		
		Intent intent = getIntent();
		search = intent.getIntExtra("search", 0);
		
		EditText familyName = (EditText) findViewById(R.id.lastnameField);
		EditText firstName = (EditText) findViewById(R.id.firstnameField);
		EditText middleName = (EditText) findViewById(R.id.middlenameField);
		EditText bDay = (EditText) findViewById(R.id.bdayField);
		EditText contactNumber = (EditText) findViewById(R.id.contactField);
		
		EditText street = (EditText) findViewById(R.id.streetField);
		EditText city = (EditText) findViewById(R.id.cityField);
		
		PersonDbHelper db = new PersonDbHelper(this);
		List<Contact> contacts = db.getAllContacts();
		
		for(Contact cn: contacts){
			if(cn.getID() == search) {
				familyName.setText(cn.getLname());
				firstName.setText(cn.getFname());
				middleName.setText(cn.getMname());
				idConfirm = cn.getID();
				
				if (cn.getGender().equalsIgnoreCase("male")) {
					RadioButton btn = (RadioButton) findViewById(R.id.radio1);
					btn.setChecked(true);
				} else {
						RadioButton btn = (RadioButton) findViewById(R.id.radio0);
						btn.setChecked(true);
					}
					
				bDay.setText(cn.getBday());
				contactNumber.setText(cn.getPhoneNumber());
				
				street.setText(cn.getStreet());
				city.setText(cn.getCity());
				
				condition = cn.getCondition();
				allergy = cn.getAllergy();
				medication = cn.getMedication();
			}
			
		}
		
        Button dateBtn = (Button) findViewById(R.id.datebtn);
        
        final OnDateSetListener odsl=new OnDateSetListener(){
            public void onDateSet(DatePicker arg0, int year, int month, int dayOfMonth) {
             // TODO Auto-generated method stub
         	   String fMonth = null;
         	   switch(month){
         	   case 0: fMonth = "Jan";
         	   			break;
         	   case 1: fMonth = "Feb";
 	   					break;
         	   case 2: fMonth = "Mar";
 	   					break;
         	   case 3: fMonth = "Apr";
 	   					break;
         	   case 4: fMonth = "May";
 	   					break;
         	   case 5: fMonth = "Jun";
 	   					break;
         	   case 6: fMonth = "Jul";
 	   					break;
         	   case 7: fMonth = "Aug";
 	   					break;
         	   case 8: fMonth = "Sep";
 	   					break;
         	   case 9: fMonth = "Oct";
 	   					break;
         	   case 10: fMonth = "Nov";
 	   					break;
         	   case 11: fMonth = "Dec";
 	   					break;
         	   }
         	   
             TextView txt=(TextView)findViewById(R.id.bdayField);
             txt.setText(dayOfMonth+" "+fMonth+" "+year);
            }};

         dateBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
             // TODO Auto-generated method stub
             Calendar cal=Calendar.getInstance();
             DatePickerDialog datePickDiag=new DatePickerDialog(EditPatientActivity.this,odsl,cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
             datePickDiag.show();
            }});
         
         db.close();
		
	}
	
	public void goNext(View view){
		PersonDbHelper db = new PersonDbHelper(this);
		List<Contact> contacts = db.getAllContacts();
		Intent intent = new Intent(this, EditMedicationsActivity.class);
		
		RadioGroup radioGroupGender;
		RadioButton radioButtonGender;
		
		radioGroupGender = (RadioGroup) findViewById(R.id.radioGroup1);
		int selectHereditary = radioGroupGender.getCheckedRadioButtonId();
		radioButtonGender = (RadioButton) findViewById(selectHereditary);
		
		EditText familyName = (EditText) findViewById(R.id.lastnameField);
		EditText firstName = (EditText) findViewById(R.id.firstnameField);
		EditText middleName = (EditText) findViewById(R.id.middlenameField);
		EditText bDay = (EditText) findViewById(R.id.bdayField);
		EditText contactNumber = (EditText) findViewById(R.id.contactField);
		
		EditText street = (EditText) findViewById(R.id.streetField);
		EditText city = (EditText) findViewById(R.id.cityField);
		
		if ( familyName.length() == 0 || firstName.length() == 0 ) {
			
			Toast.makeText(getApplicationContext(), "Please fill up the First and Last Name", Toast.LENGTH_SHORT).show();
			
		}else {
			
			String[] info = { familyName.getText().toString() , firstName.getText().toString() , middleName.getText().toString() , 
							radioButtonGender.getText().toString() , bDay.getText().toString(), contactNumber.getText().toString(),
							street.getText().toString(), city.getText().toString()};
			
			String[] meds = {condition,allergy,medication};
			
			intent.putExtra("info",info);
			intent.putExtra("meds",meds);
			intent.putExtra("id", idConfirm);
			startActivity(intent);
		}
	}
	
	protected void setOrientation() {
    	int current = getRequestedOrientation();
    	if(current != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
    		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	}
    }

}
